package com.goscom.app.deeset;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import com.goscom.app.database.DBAdapter;

public class OfferLetter extends Activity implements OnClickListener,
		OnFocusChangeListener {

	private String strMerId = "";
	private DBAdapter mDB;
	private EditText edtComDate, edtContracted, edtNonContracted, edtAddSto,
			edtPay;
	private CheckBox ckbAgree;
	private DatePickerDialog mDatePicker;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.offerletter);

		edtComDate = (EditText) findViewById(R.id.edtOffLetComDate);
		edtContracted = (EditText) findViewById(R.id.edtOffLetContracted);
		edtNonContracted = (EditText) findViewById(R.id.edtOffLetNonContracted);
		edtAddSto = (EditText) findViewById(R.id.edtOffLetAddSto);
		edtPay = (EditText) findViewById(R.id.edtOffLetPay);
		ckbAgree = (CheckBox) findViewById(R.id.ckbOffLetAgree);

		mDB = new DBAdapter(OfferLetter.this);
		mDB.open();

		getMerId();
		edtComDate.setOnClickListener(this);
		edtComDate.setOnFocusChangeListener(this);
	}

	public void getMerId() {
		strMerId = getIntent().getStringExtra("merid");
		ContentValues content = new ContentValues();
		content = mDB.queryDataByMerId("OFFERLETTER", strMerId);
		if (content != null) {
			edtComDate.setText(content.getAsString("comdate"));
			edtPay.setText(content.getAsString("pay"));
			edtContracted.setText(content.getAsString("contracted"));
			edtNonContracted.setText(content.getAsString("noncontracted"));
			edtAddSto.setText(content.getAsString("addsto"));
			if (content.getAsString("agree").equals("1")) {
				ckbAgree.setChecked(true);
			}
		} else {
			edtComDate.setText(new StringBuilder()
					.append(Calendar.getInstance().getTime().getDate())
					.append("/")
					.append(Calendar.getInstance().getTime().getMonth() + 1)
					.append("/")
					.append(Calendar.getInstance().getTime().getYear() + 1900));
		}
	}

	public void Logout(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(OfferLetter.this, Login.class));
	}

	public void goPrevious(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(OfferLetter.this, Upload.class).putExtra(
				"merid", strMerId));
	}

	public void goNext(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(OfferLetter.this, ContractedHours.class)
				.putExtra("merid", strMerId));

	}

	public void Save(View v) {
		saveDB();
		Toast.makeText(OfferLetter.this, "Save Successful!", Toast.LENGTH_LONG)
				.show();
	}

	public void saveDB() {
		if (mDB.queryMerchandiser("OFFERLETTER", strMerId) == 0) {
			insertDB();
		} else {
			mDB.deleteData("OFFERLETTER", strMerId);
			insertDB();
		}
	}

	public void insertDB() {
		ContentValues content = new ContentValues();
		content.put("merid", strMerId);
		content.put("comdate", edtComDate.getText().toString());
		content.put("pay", edtPay.getText().toString());
		content.put("agree", ckbAgree.isChecked());
		content.put("contracted", edtContracted.getText().toString());
		content.put("noncontracted", edtNonContracted.getText().toString());
		content.put("addsto", edtAddSto.getText().toString());
		mDB.insertData("OFFERLETTER", content);
	}

	@Override
	protected void onRestart() {
		mDB.open();
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		if (mDB != null) {
			mDB.close();
		}
		super.onDestroy();
	}

	private DatePickerDialog.OnDateSetListener mDateListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
			edtComDate.setText(new StringBuilder().append(selectedDay).append("/")
					.append(selectedMonth + 1).append("/").append(selectedYear));
		}
	};
	
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		switch (v.getId()) {
		case R.id.edtOffLetComDate:
			if (hasFocus) {
				mDatePicker = new DatePickerDialog(this, mDateListener,
						Calendar.getInstance().get(Calendar.YEAR), Calendar
								.getInstance().get(Calendar.MONTH), Calendar
								.getInstance().get(Calendar.DAY_OF_MONTH));
				mDatePicker.show();
			} else {

			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.edtOffLetComDate:
			mDatePicker = new DatePickerDialog(this, mDateListener,
					Calendar.getInstance().get(Calendar.YEAR), Calendar
							.getInstance().get(Calendar.MONTH), Calendar
							.getInstance().get(Calendar.DAY_OF_MONTH));
			mDatePicker.show();
			break;
		default:
			break;
		}
	}
}
